﻿namespace _15_C_中is和as关键字
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "C#关键字is和as";
            //is关键字检查对象的类型是否与给定的类型兼容
            //object obj = new object();

            //string str = "你好";

            //if (str is object)
            //{
            //    Console.WriteLine("兼容");
            //}
            //else
            //{
            //    Console.WriteLine("不兼容");
            //}
            //int a = 1;
            //double b = 2;

            //if (a is double)
            //{
            //    Console.WriteLine("兼容");
            //}
            //else
            //{
            //    Console.WriteLine("不兼容");
            //}


            //Student student = new Student();

            //if (student is Person)
            //{
            //    Console.WriteLine("兼容");
            //}
            //else
            //{
            //    Console.WriteLine("不兼容");
            //}
            //is关键字不会抛异常

            //as 关键字 强制转换

            //Person person = new Person();
            //Student student = new Student();

            //person = student is Person ? (Person)student : null;

            ////person = student as Person;
            //if (person != null)
            //{
            //    Console.WriteLine("成功");
            //}
            //else
            //{
            //    Console.WriteLine("失败");
            //}

            //int a = 1;
            //double b = 2;

            //a = b as int;
            //as 与可以为null的引用类型配合使用

            Console.ReadKey();
        }
    }
}
